package Drifter;

/**
 * Created by Aaron on 8/5/2014.
 */

import EntityPack.Entity;
import EntityPack.EntityGenerator;
import EntityPack.Player;
import MapPack.Cell;
import MapPack.Map;
import MapPack.MapGenerator;
import MaterialPack.IPortable;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;

import java.util.ArrayList;

// Updates the map and all entities.
public class Driver {

    private final Map map;
    private final Player player;
    private final EntityGenerator entityGen;
    private final MapGenerator mapGen;
    private int elapsedTime = 0;

    public Driver ( ) {
        // Generate the map.
        this.mapGen = new MapGenerator();
        mapGen.generateMap();
        this.map = mapGen.getMap();

        // Generate initial entities.
        this.entityGen = new EntityGenerator(this.map);
        entityGen.generateEntities();
        this.player = entityGen.getPlayer();
    }

    public void drive (int lastKeypress, Cell selectedCell) {
        if (!this.player.isAlive()) {
            return;
        }

        boolean validMove = true;

        // Send inputs.
        switch(lastKeypress) {
            case Keys.A:
                if (!this.player.moveLeft()) {
                    validMove = false;
                }
                break;
            case Keys.D:
                if (!this.player.moveRight()) {
                    validMove = false;
                }
                break;
            case Keys.W:
                if (!this.player.moveUp()) {
                    validMove = false;
                }
                break;
            case Keys.S:
                if (!this.player.moveDown()) {
                    validMove = false;
                }
                break;
            case Keys.T:
                break;
            case Keys.R:
                if (!this.player.refillContainers(selectedCell)) {
                    validMove = false;
                }
                break;
            case Input.Buttons.LEFT:
                if (this.player.getInventory().size() > 0) {
                    IPortable portable = this.player.getInventory().get(0);
                    if (!this.player.place(portable, selectedCell)) {
                        validMove = false;
                    }
                } else {
                    validMove = false;
                }
                break;
            case Input.Buttons.RIGHT:
                if (!this.player.pickup(selectedCell)) {
                    validMove = false;
                }
                break;
            default:
                validMove = false;
                break;
        }

        if (validMove) {
            this.player.performBodilyFunctions();
            this.elapsedTime++;
        }
    }

    public Map getMap ( ) {
        return this.map;
    }

    public Player getPlayer ( ) {
        return this.player;
    }

    public ArrayList<Entity> getEntities ( ) {
        return this.map.getEntities();
    }

    public int getElapsedTime ( ) {
        return this.elapsedTime;
    }

}
